package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 */
@Component("liShuaBenefitTask")
public class LiShuaBenefitTask {
    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private AgentService agentService;
    @Autowired
    private AgentInfoService  agentInfoService;

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private  BenefitDayService benefitDayService;
    @Autowired
    private TranTerBenefitDayService tranTerBenefitDayService;

    private Logger logger= LoggerFactory.getLogger(getClass());

    private final BigDecimal MUTILPART =new BigDecimal("10000");

    private Date today;
    private Date benefitDate;
    private String benefitDateStr;


    private void initialDate(){
         today = DateUtils.stringToDate(DateUtils.format(new Date()), DateUtils.DATE_PATTERN);
         benefitDate = DateUtils.addDateDays(today, -1);
        benefitDateStr=DateUtils.format(benefitDate,DateUtils.DATE_PATTERN);
    }

    /**
     * 考拉超收 。5的分润计算
     */
    public void dotFiveBenefit(){
        logger.info("------------立刷0.5分润计算开始-----------------");
        //获取当前日期
        initialDate();

        List<TranTransactionInfo> list = transactionInfoService.selectList(new EntityWrapper<TranTransactionInfo>()
                .eq("is_benefited",0)
                .eq("kaola_type",2)
                .eq("dept_id",1)
                .eq("terminal_type",9)
                .eq("transaction_date",benefitDateStr)

        );

        for (TranTransactionInfo info :list) {
            TerMerchantReport report = merchantService.queryMerchantBySn(info.getSn());
            if(report==null){
                continue;
            }

            if(report.getAgentId().equals(100000L)){
                continue;
            }

            SysAgent agent = agentService.queryAgentById(report.getAgentId());

            if(agent.getPid().equals(100000L)){
                topAgentBenefit(agent,info);
            }else {
                normalAgentBenefit(agent,info);
            }

        }

        List<TranTransactionInfo> list2 = transactionInfoService.selectList(new EntityWrapper<TranTransactionInfo>()
                .eq("transaction_date", benefitDateStr)
                .eq("is_benefited",0)
                .eq("kaola_type",2)
                .eq("dept_id",889323)
                .eq("terminal_type",10)
        );

        for (TranTransactionInfo info :list2) {

            TerMerchantReport report = merchantService.queryMerchantBySn(info.getSn());
            if(report==null){
                continue;
            }

            if(report.getAgentId().equals(80000L)){
                continue;
            }

            SysAgent agent = agentService.queryAgentById(report.getAgentId());

            if(agent.getPid().equals(80000L)){
                topAgentBenefit(agent,info);
            }else {
                normalAgentBenefit(agent,info);
            }
        }


        logger.info("------------立刷0.5分润计算结束-----------------");

    }

    private void topAgentBenefit(SysAgent agent, TranTransactionInfo info) {
        logger.info("------------立刷顶级盟主：-"+agent.getAgentId()+"计算分润----------------");
        BigDecimal topOwnerRate=new BigDecimal("4");

        BigDecimal amount=info.getAmount();

        BigDecimal topOwnBenefit = amount.multiply(topOwnerRate).divide(MUTILPART).setScale(2,BigDecimal.ROUND_HALF_UP);

        agentInfoService.addAmount(agent.getAgentId(),topOwnBenefit);

        benefitDayService.addSelfBenefitDay(info,topOwnBenefit,agent);

        tranTerBenefitDayService.addNewRecord(agent,info,topOwnBenefit);

        info.setIsBenefited(1);
        transactionInfoService.updateById(info);

        logger.info("------------立刷顶级盟主：-"+agent.getAgentId()+"计算分润----------------");


    }

    /**
     * 普通合伙人分润
     * @param agent
     * @param info
     */
    private void normalAgentBenefit(SysAgent agent, TranTransactionInfo info) {
        logger.info("------------立刷普通用户：-"+agent.getAgentId()+"计算分润----------------");
        //给交易所属人计算万3的分润
        BigDecimal ownerRate=new BigDecimal("3");

        BigDecimal amount=info.getAmount();

        BigDecimal ownBenefit = amount.multiply(ownerRate).divide(MUTILPART).setScale(2,BigDecimal.ROUND_HALF_UP);

        agentInfoService.addAmount(agent.getAgentId(),ownBenefit);

        benefitDayService.addSelfBenefitDay(info,ownBenefit,agent);

        tranTerBenefitDayService.addNewRecord(agent,info,ownBenefit);
        //上级推荐人
        Long pid=agent.getPid();

        //顶级盟主计算
        SysAgent baseAgent = agentService.queryBasicAgentBydeptId(agent.getDeptId());
        //顶级推荐人
        SysAgent topAgent = agentService.queryTopAgentBySubIdAndBase(agent.getAgentId(), baseAgent.getAgentId());

        if(pid.equals(topAgent.getAgentId())){
            addTopAgentBenefit(topAgent,info);
        }else {

            //直属计算万0.5的分润
            BigDecimal pidRate = new BigDecimal("0.5");

            BigDecimal pidBenefit = amount.multiply(pidRate).divide(MUTILPART).setScale(2, BigDecimal.ROUND_HALF_UP);

            SysAgent pAgent = agentService.queryAgentById(pid);

            agentInfoService.addAmount(pid, pidBenefit);

            tranTerBenefitDayService.addNewRecord(pAgent, info, ownBenefit);

            benefitDayService.addTeamBenefit(info, pAgent, pidBenefit);

            //顶级计算0.5
            BigDecimal topRate = new BigDecimal("0.5");

            BigDecimal topBenefit = amount.multiply(topRate).divide(MUTILPART).setScale(2, BigDecimal.ROUND_HALF_UP);

            agentInfoService.addAmount(topAgent.getAgentId(), topBenefit);

            tranTerBenefitDayService.addNewRecord(topAgent, info, topBenefit);

            benefitDayService.addTeamBenefit(info, topAgent, topBenefit);
        }

        info.setIsBenefited(1);
        transactionInfoService.updateById(info);

        logger.info("------------立刷普通用户：-"+agent.getAgentId()+"计算分润----------------");
    }

    private void addTopAgentBenefit(SysAgent topAgent, TranTransactionInfo info) {

        //直属+顶级计算万0.5+0.5的分润
        BigDecimal pidRate = new BigDecimal("1");
        BigDecimal amount=info.getAmount();
        Long topAgentId=topAgent.getAgentId();
        BigDecimal benefit = amount.multiply(pidRate).divide(MUTILPART).setScale(2, BigDecimal.ROUND_HALF_UP);
        SysAgent pAgent = agentService.queryAgentById(topAgentId);

        agentInfoService.addAmount(topAgentId, benefit);

        tranTerBenefitDayService.addNewRecord(pAgent, info, benefit);

        benefitDayService.addTeamBenefit(info, pAgent, benefit);


    }




}
